import csv
import matplotlib.pyplot as plt
import matplotlib
from datetime import datetime
plt.style.use('seaborn')
matplotlib.rc("font", family='Microsoft YaHei')

filename = r'D:\ui\file_data\data\sitka_weather_2018_simple.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

# 从文件中获取日期、最高温度和最低温度
    dates,highs,lows = [],[],[]
    for row in reader:
        current_date = datetime.strptime(row[2],'%Y-%m-%d')
        # current_date = row[2]
        high = int(row[5])
        low = int(row[6])
        dates.append(current_date)
        highs.append(high)
        lows.append(low)

# 根据最高温度绘制图形
# plt.style.use('seaborn')  # 有网格就无法中文显示 要放在字体设置前面
fig,ax = plt.subplots()
ax.plot(dates,highs,c='red',alpha=0.5) # alpha指定颜色的透明度，0：表示完全透明  1：表示完全不透明
ax.plot(dates,lows,c='blue',alpha=0.5)
# 方法fill_between()接受一个x值两个y值
ax.fill_between(dates,highs,lows,facecolor='green',alpha=0.3) # facecolor指定填充区域的颜色

# 设置图形的格式
ax.set_title("2018年每日最高温度",fontsize=18)
ax.set_xlabel("",fontsize=16)
fig.autofmt_xdate()  # 绘制倾斜的日期标签，避免彼此重叠
ax.set_ylabel("温度(F)",size=16)
ax.tick_params(axis='both',which='major',labelsize=16)

plt.show()